package cn.itcast.core.controller;

import cn.itcast.core.pojo.Dept;
import cn.itcast.core.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;

/**
 * @auther :hyh
 * @desc :
 * @date :2019/5/7
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/dept/list")
    public List<Dept> list() {
        List<Dept> list = deptService.list();
        return list;

    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return  deptService.get(id);
    }

    @PostMapping("/dept/add")
    private boolean add(@RequestBody Dept dept) {
        return deptService.add(dept);
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/dept/info")
    public Object getInfo() {
        List<String> services = discoveryClient.getServices();
        System.out.println(services.size());
        System.out.println(services);
        List<ServiceInstance> instances = discoveryClient.getInstances("microservicecloud-dept");
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        String serviceId = serviceInstance.getServiceId();
        URI uri = serviceInstance.getUri();
        System.out.println("host:=" + host + "...id:=" + serviceId + "...url:=" + uri);
        return serviceInstance;
    }
}
